package com.zgh.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.zgh.domain.Group;
import com.zgh.domain.Result;
import com.zgh.domain.ipageGroup;
import com.zgh.domain.ipageNews;
import com.zgh.service.GroupService;
import com.zgh.util.QiniuUtil;
import com.zgh.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result getAllGroup(){
        List<Group> groupList = groupService.findAll();
        return new Result(true,"查询成功",groupList);
    }

    @RequestMapping(value = "/findByFunctionAndRegion",method = RequestMethod.POST)
    public Result findGroupByFunction(@RequestBody String params){
        String func = JSON.parseObject(params).getString("func");
        String region = JSON.parseObject(params).getString("region");
        List<Group> groupList = groupService.findByFunctionAndRegion(func,region);
        return new Result(true,"查询成功",groupList);
    }

    @RequestMapping(value = "/searchGroup/{condition}",method = RequestMethod.GET)
    public Result searchGroup(@PathVariable String condition){
        List<Group> groupList = groupService.searchGroupByCondition(condition);
        return new Result(true,"查询成功",groupList);
    }

    @RequestMapping(value = "/findGroupById",method = RequestMethod.GET)
    public Result searchGroupById(@RequestParam("id") String id){
        Group group = groupService.searchGroupById(id);
        return new Result(true,"查询成功",group);
    }

    @RequestMapping(value = "/postGroup",method = RequestMethod.POST)
    public Result postGroup(@RequestBody String cartData){
        Group group = new Group();
        group.setName(JSON.parseObject(cartData).getString("name"));
        group.setFunction(JSON.parseObject(cartData).getString("func"));
        group.setIntroduce(JSON.parseObject(cartData).getString("des"));
        List<String> position = JSON.parseArray(JSON.parseObject(cartData).getString("position"), String.class);
        String position1 = position.get(0)+"/"+position.get(1)+"/"+position.get(2);
        group.setPosition(position1);
        group.setQrcode(JSON.parseObject(cartData).getString("qrCode"));
        group.setStatus(0);
        //System.out.println(group);
        int count = groupService.postGroup(group);
        if(count==1){
            return new Result(true,"发布成功",null);
        }else {
            return new Result(false,"发布失败",null);
        }
    }

    /**
     * 上传图片至七牛云
     * @param file file
     * @return result
     */
    @RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
    public Result post2QiuNiu(@RequestParam("image") MultipartFile file){
        //原始文件名称比如 aa.png
        String originalFilename = file.getOriginalFilename() ;
        //将原始名称修改为：唯一文件名称
//        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(originalFilename, ".");
        String fileName = StringUtil.getRandomImgName(originalFilename);
        //System.out.println(fileName);
        //上传文件，上传到哪呢？图片服务器七牛云
        //把图片发放到距离图片最近的服务器上，降低我们自身服务器的带宽消耗
        String name = QiniuUtil.upload2Qiniu(file, fileName);
        //System.out.println(name);
        return new Result(true,"上传图片成功",name);
    }

    // 分页查询
    @RequestMapping(value = "/getAllGroupInfo",method = RequestMethod.POST)
    public Result getAllNewsInfo(@RequestBody String condition){
        //System.out.println(JSON.parseObject(condition));
        String query = JSON.parseObject(condition).getString("query");
        int pagenum = JSON.parseObject(condition).getInteger("pagenum");
        int pagesize = JSON.parseObject(condition).getInteger("pagesize");
        //System.out.println(pagesize);

        ipageGroup ipageGroup = groupService.getAllGroupInfo(pagenum,pagesize,query);
        //System.out.println(ipageUser);
        if (ipageGroup!=null){
            return new Result(true,"获取新闻列表成功",ipageGroup);
        }else {
            return new Result(false, "获取新闻列表失败", null);
        }
    }

    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    public Result audit(@RequestBody String param){
//        System.out.println(param);
        String id = JSON.parseObject(param).getString("id");
        String status = JSON.parseObject(param).getString("status");
        int count = groupService.audit(id,status);
        if (count==0){
            return new Result(false,"审核失败",null);
        }else{
             return new Result(true,"审核成功",null);
        }
    }
}
